package employeeOperations;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import GUIMain.EmployeeEditor;
import GUIMain.GUIFocusManager;

/**
 * Lets the user input data for a new employee
 * @author lax116
 *
 */
public class NewLocationDialog extends JDialog{
	private JTextField nameText;
	private JCheckBox isOriginCheckBox;
	private JButton ok;
	private JButton cancel;
	private static final long serialVersionUID = 1L;
	
	public NewLocationDialog(){
		super();
		this.setTitle("Enter Location Info");
		this.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		ok = new JButton("Ok");
		ok.setPreferredSize(new Dimension(75,35));
		cancel = new JButton("Cancel");
		cancel.setPreferredSize(new Dimension(75,35));
		gbc.insets.left = 4;
		gbc.insets.top = 4;
		gbc.insets.bottom = 4;
		gbc.insets.right = 4;
		gbc.gridheight = 1;
		gbc.gridwidth = 1;
		
		nameText = new JTextField();
		nameText.setPreferredSize(new Dimension(250,25));
		isOriginCheckBox = new JCheckBox("Is Origin?");
		
		gbc.gridwidth = 1;
		gbc.gridx =0;
		gbc.gridy = 0;
		this.add(new JLabel("Name"),gbc);
		gbc.gridx = 1;
		this.add(nameText,gbc);
		gbc.gridy = 1;
		gbc.gridwidth = 2;
		gbc.gridx =0;
		this.add(isOriginCheckBox,gbc);
		gbc.gridwidth = 1;
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridBagLayout());
		gbc.gridx =0;
		gbc.gridy =0;
		buttonPanel.add(ok,gbc);
		gbc.gridx = 1;
		buttonPanel.add(cancel,gbc);
		
		gbc.gridwidth = 2;
		gbc.gridx =0;
		gbc.gridy =2;
		this.add(buttonPanel,gbc);
		
		addListeners();
		
		GUIFocusManager.setDialogProperties(this);
	}
	/**
	 * Clears fields and opens dialog
	 */
	public void showNewLocationDialog(){
		nameText.setText("");
		isOriginCheckBox.setSelected(false);
		GUIFocusManager.add(this);
	}
	private void addListeners(){
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				GUIFocusManager.remove();
			}
		});
		//Add employee data to working map 
		ok.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				//Check for correctness
				if(nameText.getText().length()<1){
					JOptionPane.showMessageDialog(null, "Name cannot be empty!");
					nameText.requestFocus();
				}else{
					boolean success = EmployeeEditor.addNewLocation(nameText.getText(),isOriginCheckBox.isSelected());
					if(success)
						GUIFocusManager.remove();
					else{
						JOptionPane.showMessageDialog(null, "A location with the same name already exists!");
						nameText.requestFocus();
					}
				}
			}
		});
	}
}
